package com.aphrodite.ui.widgets;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.aphrodite.util.StringUtil;

/**
 * Animated notification message box.
 * <p>
 * Title cannot be set for this widget
 * </p>
 * 
 * @author kentelt
 * 
 */
public final class NotificationMessage extends Widget {
	private final class Ani extends TimerTask {
		public void run() {
			while (noteY > 0) {
				noteY--;
				reDrawWidget();
			}
			try {
				Thread.sleep(waitTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			while (noteY < height + 1) {
				noteY++;
				reDrawWidget();
			}
			isVisible = false;
		}
	}

	private Image icon;
	private String message;

	private volatile int noteX;

	private volatile int noteY;

	private Image offScrImg;

	private Timer timer;

	private int waitTime = 3000;

	public NotificationMessage(Theme theme, String message) {
		this(theme, message, 1, 1, 100);
	}

	/**
	 * 
	 * @param message
	 * @param x
	 *            Point from where the notification should pop up
	 * @param y
	 *            Point from where the notification should pop up
	 * @param width
	 *            of the notification box
	 */
	public NotificationMessage(Theme theme, String message, int x, int y, int width) {
		super(theme, width, 0);
		this.x = x;
		this.y = y;
		this.message = message;
		updateModel();
		isVisible = false;
	}

	public void cleanUp() {
		offScrImg = null;
		message = null;
		icon = null;
	}

	public String getMessage() {
		return message;
	}

	public void render(Graphics g) {
		if (isVisible) {
			capture(g, width, height, false);
			g.drawImage(offScrImg, noteX, noteY, 0);
			release(g, width, height, false);
		}
	}

	/**
	 * Time to wait before hiding the notification box
	 * 
	 * @param int Delay in milliseconds. The default is 5000
	 */
	public void setDelay(int delay) {
		this.waitTime = delay;

	}

	/**
	 * Set an image for the notification box
	 * 
	 * @param image
	 */
	public void setImage(Image image) {
		this.icon = image;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	/**
	 * Set the Dialog message
	 * 
	 * @param message
	 */
	public void setMessage(String message) {
		this.message = message;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	public void setVisible(boolean isVisible) {
		if (isVisible) {
			show();
		} else {
			isVisible = false;
			timer = null;
			reDrawWidget();
		}
		stateChanged(this);
	}

	public final void setTitle(String title) {

	}

	/**
	 * show the notification dialog
	 */
	public void show() {
		if (!isVisible) {
			isVisible = true;
			timer = new Timer();
			timer.schedule(new Ani(), 0);
		}
	}

	protected void updateModel() {
		String[] lines = StringUtil.doTextWrap(message, theme.widgetFont, (width - 4)
				- (icon != null ? icon.getWidth() + 2 : 0), width - 4);
		System.out.println("Font height " + theme.widgetFont.getHeight());
		/* Top padding */
		height = 2;
		int totalLines = lines.length;
		int startLine = 0;

		if (icon != null) {
			if (icon.getHeight() > theme.widgetFont.getHeight()) {
				height += icon.getHeight() + 2;
				/* The first line will be contained along with image */
				totalLines--;
			}
		}
		height += totalLines * theme.widgetFont.getHeight();

		offScrImg = Image.createImage(width, height);
		Graphics g = offScrImg.getGraphics();
		theme.renderBG(g, width, height);

		g.setFont(theme.widgetFont);
		g.setColor(theme.fgColor);
		int currY = 2;
		if (icon != null) {
			g.drawImage(icon, 2, 2, 0);
			g
					.drawString(lines[0], 4 + icon.getWidth(),
							2 + (icon.getWidth() / 2 - theme.widgetFont.getHeight() / 2), 0);
			startLine = 1;
			currY += icon.getHeight() + 2;
		}
		for (int i = startLine; i < lines.length; i++) {
			g.drawString(lines[i], 2, currY, 0);
			currY += theme.widgetFont.getHeight();
		}
		noteX = 0;
		noteY = height - 1;

	}

}
